package gui.mainWindow.ranking;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import javax.swing.JPanel;

public class RankingPanel extends JPanel implements Printable {
	private PageFormat pageFormat;
	private final int headerspace = 60;
	private final int footerspace = 90;

	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
		System.out.println("TablePrinter: print() called");

		/**
		 * set up the page
		 */

		Graphics2D graphics2D = (Graphics2D) graphics;
		int leftmostImageable = (int) pageFormat.getImageableX();
		int topmostImageable = (int) pageFormat.getImageableY();
		int imageableWidth = (int) pageFormat.getImageableWidth();
		int imageableHeight = (int) pageFormat.getImageableHeight();
		if (this.pageFormat == null) {
			this.pageFormat = (PageFormat) pageFormat.clone();
			Paper paperJTable = this.pageFormat.getPaper();
			if (this.pageFormat.getOrientation() == PageFormat.PORTRAIT) {
				paperJTable.setImageableArea(leftmostImageable,
				/* skip header area */
				topmostImageable + this.headerspace, imageableWidth,
				/* reserve space for header and footer */
				imageableHeight - this.footerspace);
			} else {
				paperJTable.setImageableArea(
				/* skip header area */
				topmostImageable + this.headerspace, leftmostImageable,
				/* reserve space for header and footer */
				imageableHeight - this.footerspace, imageableWidth);
			}
			this.pageFormat.setPaper(paperJTable);
		}

		Font font = graphics2D.getFont();

		if (pageIndex != 0) {
			return NO_SUCH_PAGE;
		}

		graphics2D.translate(this.pageFormat.getImageableX(), this.pageFormat.getImageableY());

		this.paint(graphics2D);
		return PAGE_EXISTS;
	}
}
